{% extends "admin/base.html" %}



{% block admin_content %}
<div class="card shadow mb-4">
    <div class="card-header py-3 d-flex justify-content-between align-items-center">
        <h6 class="m-0 font-weight-bold text-primary">邀请码管理</h6>
    </div>
    
    <!-- 创建邀请码表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <h5 class="card-title">创建邀请码</h5>
            <form id="createCodeForm" class="row g-3">
                <div class="col-md-6">
                    <input type="text" class="form-control" id="code" name="code" placeholder="输入邀请码（可选，留空将自动生成）">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary">创建</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 批量操作 -->
    <div class="card mb-4">
        <div class="card-body">
            <h5 class="card-title">批量操作</h5>
            <div class="row g-3">
                <div class="col-md-6">
                    <form id="importForm" class="d-flex align-items-center" enctype="multipart/form-data">
                        <input type="file" class="form-control me-2" id="importFile" accept=".xlsx" required>
                        <button type="submit" class="btn btn-success">导入</button>
                    </form>
                    <small class="text-muted">请上传Excel文件，第一列应为邀请码，支持批量导入</small>
                </div>
                <div class="col-md-6">
                    <div class="d-flex align-items-center">
                        <a href="/admin/invite_codes/export" class="btn btn-info me-2">导出未使用的邀请码(Excel)</a>
                        <a href="/admin/invite_codes/template" class="btn btn-secondary">下载导入模板</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 邀请码列表 -->
    <div class="card">
        <div class="card-body">
            <h5 class="card-title">邀请码列表</h5>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>邀请码</th>
                            <th>状态</th>
                            <th>创建者</th>
                            <th>创建时间</th>
                            <th>使用者</th>
                            <th>使用时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for code in codes %}
                        <tr>
                            <td>{{ code.code }}</td>
                            <td>
                                {% if code.is_used %}
                                <span class="badge bg-success">已使用</span>
                                {% else %}
                                <span class="badge bg-secondary">未使用</span>
                                {% endif %}
                            </td>
                            <td>{{ code.created_by }}</td>
                            <td>{{ code.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            <td>{{ code.used_by or '-' }}</td>
                            <td>{{ code.used_at.strftime('%Y-%m-%d %H:%M:%S') if code.used_at else '-' }}</td>
                            <td>
                                {% if not code.is_used %}
                                <button class="btn btn-danger btn-sm delete-code" data-id="{{ code.id }}">删除</button>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block scripts %}
<script>
$(document).ready(function() {
    // 创建邀请码
    $('#createCodeForm').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: '/admin/invite_codes/create',
            method: 'POST',
            data: $(this).serialize(),
            success: function(response) {
                alert(response.message);
                location.reload();
            },
            error: function(xhr) {
                alert(xhr.responseJSON.error);
            }
        });
    });

    // 删除邀请码
    $('.delete-code').on('click', function() {
        if (!confirm('确定要删除这个邀请码吗？')) {
            return;
        }
        
        const id = $(this).data('id');
        $.ajax({
            url: `/admin/invite_codes/${id}/delete`,
            method: 'POST',
            success: function(response) {
                alert(response.message);
                location.reload();
            },
            error: function(xhr) {
                alert(xhr.responseJSON.error);
            }
        });
    });

    // 导入邀请码
    $('#importForm').on('submit', function(e) {
        e.preventDefault();
        const formData = new FormData();
        formData.append('file', $('#importFile')[0].files[0]);
        
        $.ajax({
            url: '/admin/invite_codes/import',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                $('#importFile').val('');
                $('#messageModal .modal-body').text(response.message);
                $('#messageModal').modal('show');
                setTimeout(function() {
                    location.reload();
                }, 1500);
            },
            error: function(xhr) {
                $('#messageModal .modal-body').text(xhr.responseJSON.error);
                $('#messageModal').modal('show');
            }
        });
    });
});
</script>
{% endblock %} 